package com.yangjie.autotest.component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.context.annotation.Scope;

import com.yangjie.autotest.context.ContextIF;
import com.yangjie.autotest.exception.ValidateFailedException;
import com.yangjie.autotest.testcase.xml.Param;
import com.yangjie.autotest.testcase.xml.Params;
import com.yangjie.log.LoggerIF;

@Named("logValidator")
@Scope("prototype")
public class LogFileValidator {
	@Inject
	private LoggerIF logger = null;
	
	String basePath = null;
	public void init(String basePath) {
		this.basePath = basePath;
	}
	
	public Date getLogDate(String log) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = log.substring(0, 19);
		Date retval;
		try {
			retval = sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return retval;
	}
	
	private String getFileName(Params params) {
		for(Param param: params.getParam()) {
			if(param.getName().equalsIgnoreCase("filename")) {
				return param.getValue();
			}
		}
		return null;
	}
	
	private List<String> getExpectedLogs(Params params) {
		List<String> logList = new LinkedList<String>();
		for(Param param: params.getParam()) {
			if(param.getName().equalsIgnoreCase("log")) {
				logList.add(param.getValue());
			}
		}
		return logList;
	}
//	
//	public void checkLog(ContextIF context, Params params)throws ValidateFailedException {
//		int timeout = 0;
//		while(timeout < 300000) {
//			try {
//				if(doValidateLog(context, params)) {
//					return;
//				}
//			} catch(ValidateFailedException e) {
//				if(timeout >= 300000) {
//					throw e;
//				}
//				try {
//					Thread.sleep(10000);
//				} catch (InterruptedException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
//				timeout += 10000;
//			}
//		}
//	}
//	private boolean doValidateLog(ContextIF context, Params params)throws ValidateFailedException {
//		String fileName = getFileName(params);
//		if(fileName == null) {
//			throw new ValidateFailedException("No filename configured in validate rule...");
//		}
//		List<String> logList = getExpectedLogs(params);
//		for(String log: logList) {
//			validateSingleLog(context, fileName, log);
//		}
//
//		return true;
//	}
	
//	private void validateSingleLog(ContextIF context, String fileName, String expectedLog) throws ValidateFailedException {
//		try {
//			File file = new File(basePath + File.separator + fileName);
//			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
//			String line = br.readLine();
//			Date testCaseStartDate = (Date)context.getProperty(HPSAContextProperty.START_DATE_OF_TEST_CASE);
//			while(line != null) {
//				Date logDate = getLogDate(line);
//				if(logDate != null) {
//					if(!logDate.before(testCaseStartDate)) {
//						if(line.contains(expectedLog)) {
//							logger.debug("Matched log: line[" + line + "]; expectedLog[" + expectedLog + "]");
//							return;
//						}
//					}
//				}
//				line = br.readLine();
//			}
//			throw new ValidateFailedException("Validate log failed. Expected:" + expectedLog);
//		} catch(Exception e) {
////			e.printStackTrace();
//			throw new ValidateFailedException("Validate log failed. Expected:" + expectedLog);
//		}
//	}
}
